package com.ce.adapter;



import org.json.JSONArray;
import org.json.JSONObject;

import com.ce.activity.R;
import com.ce.activity.SoftApplication;
import com.ce.utils.AppUtils;
import com.ce.utils.AsyncImageLoader;
import com.ce.utils.DishDefine;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryJsonAdapter extends BaseAdapter{
	private final static int mResDef = R.drawable.pic_dish_default;
	private Context mContext;
	private String mImgFile;
	private JSONArray mJsonArray;
	private int mCount;
	private String mImgPath;
	private View mView;
	private AsyncImageLoader imageLoader;
	public GalleryJsonAdapter(Context context
						, View view
						, JSONArray jsonArray
						, String imgFile 
						, String imgPath){
		this.mJsonArray = jsonArray;
		this.mView = view;
		this.mImgFile = imgFile;
		this.mContext = context;
		this.mImgPath = imgPath;
		this.mCount = mJsonArray == null ? 0 : mJsonArray.length();
		this.mCount = this.mCount > 4 ? 4 : this.mCount; 
		this.imageLoader = ((SoftApplication)mContext).getImageLoader();
	}
	
	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}
	
	public int getArrayAcount() {
		return this.mCount;
	}

	@Override
	public Object getItem(int position) {
		if(mCount == 0)
			return null;
		position = position % mCount;
		return mJsonArray.opt(position); 
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(mCount == 0)
			return null;
		position = position % mCount;
		JSONObject jsonObject = mJsonArray.optJSONObject(position);
		if(jsonObject == null)
			return null;
		ImageView view = null;
		if(convertView == null) {
			view = new ImageView(mContext);
			view.setLayoutParams(new Gallery.LayoutParams(
					AppUtils.getUnitDipSize(mContext, 180), AppUtils.getUnitDipSize(mContext, 120)));
			view.setBackgroundResource(R.drawable.pic_frame);
		}else {
			view = (ImageView) convertView;
		}
		String imgPath = jsonObject.optString(mImgPath);
		if(imgPath == null || "".equals(imgPath)) {
			setViewImage(view, mResDef);
		}else {
			view.setTag(DishDefine.SERVER_IMG_URL + imgPath);
			setViewImage(view,DishDefine.SERVER_IMG_URL + imgPath);
		}
		return view;
	}
	
	public void setViewImage(final ImageView v, int res) {
		v.setImageResource(res);
	}
	
	public void setViewImage(final ImageView v, String value) {
		Drawable cacheImage = imageLoader.loadDrawable(mContext, mImgFile,
				value, new AsyncImageLoader.ImageCallback() {
					@Override
					public void imageLoaded(Drawable imageDrawable,
							String imgPath) {
						ImageView imgTag = (ImageView) mView.findViewWithTag(imgPath);
						if (imageDrawable != null && imgTag != null) {
							imgTag.setImageDrawable(imageDrawable);
						}
					}
				});
		if (cacheImage == null) {
			v.setImageResource(mResDef);
		} else {
			v.setImageDrawable(cacheImage);
		}
	}
}
